package com.example.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.entity.SOrder;
import com.example.service.SOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private SOrderService sOrderService;

    /**
     * 每60s
     * 清除超时未付款订单
     */
    @Scheduled(fixedRate = 60000)
    public void orderTask() {
        log.info("清除超时未付款订单");
        LambdaQueryWrapper<SOrder> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(SOrder::getState, 0);
        updateWrapper.lt(SOrder::getUnpaidTime, LocalDateTime.now().minusMinutes(30));
        sOrderService.remove(updateWrapper);
    }
}
